/*
 * Copyright © 2011 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.classes;

import javax.microedition.lcdui.*;
import com.senseforce.midlets.PhoneBook;

/**
 *
 * @author jcccn
 * @site http://www.senseforce.com/
 */
public class PhoneBookAddForm extends Form implements CommandListener {
	
	private PhoneBook midlet = null;
	private Command cancelCommand = new Command("Cancel", Command.BACK, 0);
	private Command okCommand = new Command("OK", Command.SCREEN, 0);
	
	private TextField nameTextField = new TextField("contact name", null, 20, TextField.ANY);
	private TextField phoneTextField = new TextField("phone number", null, 20, TextField.PHONENUMBER);
	
	public PhoneBookAddForm(PhoneBook midlet) {
		super("Add a new contact");
		this.midlet = midlet;
		setCommandListener(this);
		addCommand(cancelCommand);
		addCommand(okCommand);
		
		append(nameTextField);
		append(phoneTextField);
	}
	
	public void addContact(String name, String phonenumber) {
		PhoneBookContact contact = new PhoneBookContact();
		contact.setCname(name);
		contact.setPhone(phonenumber);
		PhoneBookRMSManager.addContact(contact);
	}
	
	public void refresh() {
		nameTextField.setString(null);
		phoneTextField.setString(null);
	}
	
	public void commandAction(Command c, Displayable d) {
		if (c == okCommand) {
			addContact(nameTextField.getString(), phoneTextField.getString());
		}
		refresh();
		midlet.changeForm(PhoneBookConfig.FORMTYPE_PHONELIST);
	}
}
